package com.dji.sdk.self.internal.controller;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.dji.sdk.self.R;
import com.dji.sdk.self.internal.utils.ToastUtils;

import dji.sdk.base.BaseProduct;

public class GetLiveStreamUrlActivity extends BaseActivity {
    private static BaseProduct mProduct;
    private TextView tvUrl;
    private GetUrl getUrl;
    private Button btnGetUrl;
    private String url;
    private Toolbar mToolbar;
    private SharedPreferences preferences;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 100){
                tvUrl.setText(url);
            }
        }
    };


    //获取无人机
    public static synchronized BaseProduct getProductInstance() {
        if (null == mProduct) {
            mProduct = DJISampleApplication.getProductInstance();
        }
        return mProduct;
    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_stream_url);
        tvUrl = findViewById(R.id.url);
        btnGetUrl = findViewById(R.id.btn_getUrl);

        preferences = getSharedPreferences("LiveStreamUrl",0);

        mToolbar = findViewById(R.id.url_toolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               finish();
            }
        });
        btnGetUrl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!TextUtils.isEmpty(url)){
                    ToastUtils.setResultToToast("已获取"+url);
                }else{
                    getUrl = new GetUrl(GetLiveStreamUrlActivity.this);
                    Log.i("SendHttp","getUrl onDataChange");
                    getUrl.setCallback(new GetUrl.Callback() {
                        @Override
                        public void onDataChange(String pushUrl) {
                            url = pushUrl;
                            if(!TextUtils.isEmpty(url)){
                                preferences.edit().putString("liveUrl",url).apply();
                                tvUrl.setText(url);
                                Log.i("GetUrl","getUrl onDataChange"+url);
                            }

                        }
                    });
                }
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();


    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}